package com.surelution.projects.crm

class MessageBoardController {

    static allowedMethods = [save: "POST", update: "POST", delete: "POST"]
	def springSecurityService
	
    def index = {
        redirect(action: "list", params: params)
    }

    def list = {
        params.max = Math.min(params.max ? params.int('max') : 8, 100)
		def messageBoardList = MessageBoard.createCriteria().list([max:params.max, offset:params.offset]) {
			isNull("replyTo")
			order("createTime", "desc")
		}
        [messageBoardInstanceList: messageBoardList, messageBoardInstanceTotal: messageBoardList.totalCount]
    }
	
	def publicList = {
		params.max = Math.min(params.max ? params.int('max') : 8, 100)
		def messageBoardList = MessageBoard.createCriteria().list([max:params.max, offset:params.offset]){
			isNull("replyTo")
			eq("isPublic",true)
			order("createTime", "desc")
		}
		[messageBoardInstanceList: messageBoardList, messageBoardInstanceTotal: messageBoardList.totalCount]
	}

    def create = {
        def messageBoardInstance = new MessageBoard()
		def id = params.id
		def replyList
        messageBoardInstance.properties = params
		if(id) {
			def m = MessageBoard.get(id)
			messageBoardInstance.replyTo = m
			replyList = MessageBoard.findAllByReplyTo(m)
		}
        return [messageBoardInstance: messageBoardInstance,replyList:replyList]
    }
	def newPublic = {
		def messageBoardInstance = new MessageBoard()
		messageBoardInstance.properties = params
		messageBoardInstance.isPublic = true
		return [messageBoardInstance: messageBoardInstance]
	}

	def savePublic = {
		def messageBoardInstance = new MessageBoard(params)
		messageBoardInstance.createTime = new Date()
		messageBoardInstance.user = springSecurityService.principal.username
		messageBoardInstance.station = User.findByUsername(springSecurityService.principal.username).station
		messageBoardInstance.isPublic = true
		if (messageBoardInstance.save(flush: true)) {
			flash.message = "${message(code: 'default.created.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), messageBoardInstance.id])}"
           	redirect(action: "show",id: messageBoardInstance.id)
		}
		else {
			render(view: "create", model: [messageBoardInstance: messageBoardInstance])
		}
	}
	
    def save = {
        def messageBoardInstance = new MessageBoard(params)
		messageBoardInstance.createTime = new Date()
		messageBoardInstance.user = springSecurityService.principal.username
		messageBoardInstance.station = User.findByUsername(springSecurityService.principal.username).station
		messageBoardInstance.isPublic = false
        if (messageBoardInstance.save(flush: true)) {
            flash.message = "${message(code: 'default.created.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), messageBoardInstance.id])}"
           	redirect(action: "show",id: messageBoardInstance.id)
        }
        else {
            render(view: "create", model: [messageBoardInstance: messageBoardInstance])
        }
    }

    def show = {
        def messageBoardInstance = MessageBoard.get(params.id)
		def replyList = MessageBoard.findAllByReplyTo(messageBoardInstance)
        if (!messageBoardInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), params.id])}"
            redirect(action: "list")
        }
        else {
            [messageBoardInstance: messageBoardInstance,replyList:replyList]
        }
    }

	def isPublic = {
        def messageBoardInstance = MessageBoard.get(params.id)
		flash.message = "${message(code: 'default.updated.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), messageBoardInstance.id])}"
		if (messageBoardInstance) {
			messageBoardInstance.isPublic = true
			redirect(action: "list")
		}
		else {
			redirect(action: "list")
		}
	}
	
	def publicIt = {
		def messageBoardInstance = MessageBoard.get(params.id)
		flash.message = "${message(code: 'default.updated.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), messageBoardInstance.id])}"
		if (messageBoardInstance) {
			messageBoardInstance.isPublic = false
			redirect(action: "list")
		}
		else {
			redirect(action: "list")
		}
	}
	
    def edit = {
        def messageBoardInstance = MessageBoard.get(params.id)
        if (!messageBoardInstance) {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), params.id])}"
            redirect(action: "list")
        }
        else {
            return [messageBoardInstance: messageBoardInstance]
        }
    }

    def update = {
        def messageBoardInstance = MessageBoard.get(params.id)
        if (messageBoardInstance) {
            if (params.version) {
                def version = params.version.toLong()
                if (messageBoardInstance.version > version) {
                    
                    messageBoardInstance.errors.rejectValue("version", "default.optimistic.locking.failure", [message(code: 'messageBoard.label', default: 'MessageBoard')] as Object[], "Another user has updated this MessageBoard while you were editing")
                    render(view: "edit", model: [messageBoardInstance: messageBoardInstance])
                    return
                }
            }
            messageBoardInstance.properties = params
			messageBoardInstance.isPublic = false
            if (!messageBoardInstance.hasErrors() && messageBoardInstance.save(flush: true)) {
                flash.message = "${message(code: 'default.updated.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), messageBoardInstance.id])}"
                redirect(action: "list")
            }
            else {
                render(view: "edit", model: [messageBoardInstance: messageBoardInstance])
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), params.id])}"
            redirect(action: "list")
        }
    }

    def delete = {
        def messageBoardInstance = MessageBoard.get(params.id)
		println messageBoardInstance
        if (messageBoardInstance) {
            try {
                messageBoardInstance.delete(flush: true)
                flash.message = "${message(code: 'default.deleted.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), params.id])}"
                redirect(action: "list")
            }
            catch (org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "${message(code: 'default.not.deleted.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), params.id])}"
                redirect(action: "show", id: params.id)
            }
        }
        else {
            flash.message = "${message(code: 'default.not.found.message', args: [message(code: 'messageBoard.label', default: 'MessageBoard'), params.id])}"
            redirect(action: "list")
        }
    }
	
}
